#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#include <ctype.h>
#include <arpa/inet.h>

#define SERV_PORT 7777

void err(char *msg,int exitCode){
    perror(msg);
    if(exitCode == 0){
        return;
    }
    exit(1);
}

int main(){
    int cfd;
    struct sockaddr_in serv_addr;
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(SERV_PORT);
    inet_pton(AF_INET,"127.0.0.1",&serv_addr.sin_addr.s_addr);

    cfd = socket(AF_INET,SOCK_STREAM,0);
    if(cfd == -1){
        err("socket error",1);
    }

    int ret = connect(cfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr));
    if (ret == -1){
        err("connect error",1);
    }
    int counter = 10;
    char buf[1024];
    while(--counter){
        write(cfd,"hello",5);
        ret = read(cfd,buf,sizeof(buf));
        write(STDOUT_FILENO,buf,ret);
        sleep(1);
    }
    close(cfd);
    return 0;

}